package prepro;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.SortedSet;
import java.util.Stack;
import java.util.Vector;

import javax.imageio.stream.FileImageInputStream;

import myLucene.GetTime;

public class SerializeStopWordObject {

	/**
	 * @param args
	 * @throws IOException
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		// TODO Auto-generated method stub
		BufferedReader stopsBr = new BufferedReader(new FileReader(new File(
				"/local/joofeit/german_stop_words_sorted")));
		BufferedReader terms = new BufferedReader(new FileReader(new File(
				"/local/joofeit/termsToIntPunt")));
		HashMap<String, Integer> termsMap = new HashMap<String, Integer>();
		String line = "";
		while ((line = terms.readLine()) != null) {
			String[] lineSplit = line.trim().split(" ");
			termsMap.put(lineSplit[1], Integer.parseInt(lineSplit[0]));
		}
		String stops = "";
		LinkedList<Integer> ll = new LinkedList<Integer>();

		while ((stops = stopsBr.readLine()) != null) {
			if (termsMap.containsKey(stops.trim().toLowerCase())){
			ll.add(termsMap.get(stops.trim().toLowerCase()));
			}else{
				System.out.println("Not in Map: "+stops.trim().toLowerCase());
			}
		}
		System.out.println("Number of StopWords: " + ll.size());

		Collections.sort(ll);
		Collections.reverse(ll);
		System.out.println("Number of StopWords: " + ll.size());

		Stack stack = getStack(ll);
		serializeStops(stack);
		Stack<Integer> stack2 =deserializeStops();
		printStack(stack2);

	}

	public static void serializeStops(Stack stack)
			throws FileNotFoundException, IOException {
		System.out.println("Start serializeStops");
		ObjectOutputStream o_out = new ObjectOutputStream(new FileOutputStream(
				new File("/local/joofeit/stops.data")));
		o_out.writeObject(stack);
		o_out.close();
		

	}

	public static Stack<Integer> getStack(LinkedList<Integer> ll) {
		Stack<Integer> stack = new Stack<Integer>();
		for (int i : ll) {
			stack.add(i);
		}
		return stack;

	}

	public static Stack deserializeStops() throws FileNotFoundException,
			IOException, ClassNotFoundException {
		System.out.println("Start deserializeStops");
		long start = System.currentTimeMillis();
		File file = new File("/local/joofeit/stops.data");
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
		Stack<Integer> stack = (Stack<Integer>) in.readObject();
		System.out.println(GetTime.getDuration(start, System
				.currentTimeMillis()));
		return stack;
	}
	public static void printStack(Stack<Integer> stack){
		for(int i : stack){
			System.out.println(i);
		}
	}
}
